package com.huaxin.common.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.R;

public class BottomSheetDialog extends Dialog implements View.OnClickListener {
    private final Context              mContext;
    private final LayoutInflater       mInflater;
    private       RecyclerView.Adapter mAdapter;
    private       OnItemClickListener  mListener;
    private       String[]             mDatas;
    private       TextView             mTilte;
    private       TextView             mCancel;

    public BottomSheetDialog(Context context, BaseQuickAdapter adapter, final OnItemClickListener listener) {
        super(context, R.style.Translucent_NoTitle);
        //添加条目点击事件
        mContext = context;
        mAdapter = adapter;
        mListener = listener;
        mInflater = LayoutInflater.from(mContext);


    }

    public BottomSheetDialog(Context context, String[] datas, OnItemClickListener listener) {
        super(context, R.style.Translucent_NoTitle);
        mContext = context;
        mDatas = datas;
        mInflater = LayoutInflater.from(mContext);
        mAdapter = new MyAdapter();
        mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_my_select);
        Window win = getWindow();
        win.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        win.setGravity(Gravity.BOTTOM);
        RecyclerView listview = (RecyclerView) findViewById(R.id.listView);
        listview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        listview.setAdapter(mAdapter);
        mCancel = (TextView) findViewById(R.id.cancel);
        mTilte = (TextView) findViewById(R.id.dialog_title);
        mCancel.setOnClickListener(this);
    }

    /**
     * 设置dialog标题是否可见
     *
     * @param isVisibility
     */
    public BottomSheetDialog setTitleVisibility(boolean isVisibility) {
        if (isVisibility) {
            mTilte.setVisibility(View.VISIBLE);
        } else {
            mTilte.setVisibility(View.GONE);
        }
        return this;
    }

    public BottomSheetDialog setTitle(String text) {
        mTilte.setText(text);
        return this;
    }

    /**
     * 设置标题颜色
     *
     * @param colorId
     */
    public BottomSheetDialog setTitleColor(int colorId) {
        mTilte.setTextColor(mContext.getResources().getColor(colorId));
        return this;
    }

    /**
     * 设置取消按钮颜色
     *
     * @param colorId
     */
    public BottomSheetDialog setCancleColor(int colorId) {
        mCancel.setTextColor(mContext.getResources().getColor(colorId));
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cancel) {
            dismiss();
        }
    }

    protected class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(mInflater.inflate(R.layout.item_select, parent, false));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.update(position);
        }


        @Override
        public int getItemCount() {
            return mDatas.length;
        }


        public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            View     itemView;
            TextView item;
            int      pos;

            public MyViewHolder(View itemView) {
                super(itemView);
                this.itemView = itemView;
                item = (TextView) itemView.findViewById(R.id.item);
            }

            public void update(int position) {
                this.pos = position;
                item.setText(mDatas[position]);
                itemView.setOnClickListener(this);


            }

            @Override
            public void onClick(View v) {
                mListener.onItemClick(pos);
                dismiss();
            }
        }

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
